package hn.cch.springboot_redisson.runnable;


import hn.cch.springboot_redisson.distributed.DistributedLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class LockRunnable implements Runnable {

    private static Logger logger = LoggerFactory.getLogger(LockRunnable.class);

    private static String lockKey = "lock_unlock";
    private static int leaseTime = 5;

    private static int lockCount = 50;
    private static int unlockCount = 50;


    private DistributedLock distributedLock;

    private CountDownLatch countDownLatch;

    public LockRunnable(DistributedLock distributedLock, CountDownLatch countDownLatch) {
        this.distributedLock = distributedLock;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        try {
            countDownLatch.await();
            logger.info("count await");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        unlockCount--;
        logger.info("unlockCount:{}", unlockCount);
        distributedLock.lock(lockKey, leaseTime, TimeUnit.SECONDS);
        lockCount--;
        logger.info("lockCount:{}", lockCount);
        distributedLock.unlock(lockKey);




    }
}
